package com.eop.managedBean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;

import com.eop.entity.Schnario;

@ManagedBean(name = "eop_showSchnarioBean", scope = ManagedBeanScope.REQUEST)
public class ShowSchnarioBean extends BaseSchnarioBean {
	@Accessible
	@SaveState
	private String id = FacesContext.getCurrentInstance().getExternalContext()
			.getRequestParameterMap().get("id");

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			if (null != id && !"".equals(id)) {
				sr = schnarioService.getSchnarioById(id);
				this.getSchnarioProperty(sr);
			}
		}
	}

	@Bind(id = "historySchGrid", attribute = "binding")
	private UIDataGrid historySchGrid;

	/**
	 * 取历史版本的预案
	 * 
	 * @return
	 */
	@DataModel(id = "historySchGrid")
	private List<Schnario> getHistorySchnarioGridValues() {
		List<Schnario> models = new ArrayList<Schnario>();
		if (id != null && !"".equals(id)) {
			Schnario model = schnarioService.getSchById(id);
			String relevanceId = model.getSrLastSchnario();
			this.addHistorySch(relevanceId, models);
		}
		return models;
	}

	private void addHistorySch(String relevanceId, List<Schnario> models) {
		boolean flag = true;
		while (flag) {
			if (relevanceId != null && !"".equals(relevanceId)) {
				Schnario modelRelevance = schnarioService.getSchById(relevanceId);
				if (modelRelevance != null) {
					relevanceId = modelRelevance.getSrLastSchnario();
					models.add(modelRelevance);
				} else {
					flag = false;
				}
			} else {
				flag = false;
			}
		}
	}

	@Action(id="deleteHistorySch")
	public void deleteHistorySch(){
	
	}

}
